package com.ketti.team;

import java.util.HashMap;

import android.app.TabActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

/**
 * @author JaeGwang Ryu
 * @since 		@version
 * 2010,04,23 	0.10	Create tab that height is height of text.
 * 2010,04,23	0.11	Apply background image for tab.
 * 2010,04,25	0.12	Create view that connected with tab.
 * 						And show this app_name and filename to app title.
 * 
 * 2010,04,27	0.1?	Create NEW class "SourceView" and cut out some UI codes,
 * 						put them into NEW xml file(source_view.xml)
 * */
public class EditorActivity extends TabActivity implements TabHost.TabContentFactory
{
	// This is widget to have tabWidget.
	private TabHost mTabHost;
	// This is repository that have filename(tag of tab) and sourceView.
	private HashMap<String, View> mSourceView;	
		
	/* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
              
        /* setting tabs */
        mTabHost = getTabHost();
        // Show this app_name and filename to app title.
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
		{
			@Override
			public void onTabChanged(String tabId)
			{
				// TODO Auto-generated method stub
				Log.i("xxxxxxxxxxx","Focused [" + tabId + "] tab");
				setTitle(getResources().getText(R.string.app_name)+ " - " + tabId);
				
			}
		});
        mSourceView = new HashMap<String, View>();
        
        LayoutInflater.from(this).inflate(R.layout.main,
        		mTabHost.getTabContentView(), true);
          
        /* When app begin, show '+' tab only.*/
        createTabAndSourceView("+");        
        createTabAndSourceView("This is a test.");
        createTabAndSourceView("Sure!!");        
      }       

	/**
     * create Tab and SourceView
     * @param filename : If selecting New-menu, put "*" instead of filename.
     * 					 If selecting Open-menu, put filename.
     * */
    public void createTabAndSourceView(String filenamge)
    {
    	mSourceView.put(filenamge, createSourceView(filenamge));
       	mTabHost.addTab(createTab(filenamge)); 
     }
    
    /**
     * create new tab (Tab label is tag.)
     * @param tag : If selecting New-menu, put "*" instead of filename.
     * 				If selecting Open-menu, put filename.
     * */
    public TabSpec createTab(String tag)
    {	
    	return	mTabHost.newTabSpec(tag)
    			.setIndicator(createIndicatorViewOnlyLabel(tag))
    			.setContent(this);
    }
	
	/** 
	 * Create tab widget only label without icon.
	 * 
	 * @param label If selecting New-menu, put "*" instead of filename.
     * 				If selecting Open-menu, put filename.
	 * @return tab widget only label
	 */
	public View createIndicatorViewOnlyLabel(String label) 
	{
		// create TextView for label
		TextView tv = new TextView(this);
		tv.setGravity(Gravity.CENTER);						// set on center
		tv.setText(label);									// set text of label
		tv.setBackgroundResource(R.drawable.tab_indicator);	// set tab image
		
        // If Android version 1.6 of your device, show other tab image.
        if (this.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT)
        {
            // Donut apps get old color scheme
            tv.setBackgroundResource(R.drawable.tab_indicator_v4);
            tv.setTextColor(this.getResources().getColorStateList(R.color.tab_indicator_text_v4));
        }
        return tv;
    }
	
	/**
	 * Create new sourceView.
	 * 
	 * @param filename If selecting New-menu, put "*" instead of filename.
     * 				   If selecting Open-menu, put filename.
     * @return EditText and first screen is TextView for title.
	 * */
	public View createSourceView(String filename)
	{
		Log.i("xxxxxxxxxxx","Created [" + filename + "] view");
		View v;
		// create titleView
		if(mSourceView.isEmpty())
		{			
			TextView titleView = new TextView(this);
			titleView.setText("Android Editor ver 0.12");
			titleView.setGravity(Gravity.CENTER_HORIZONTAL
								|Gravity.CENTER_VERTICAL);
			v = titleView;
		}
		
		// create sourceView
		else
		{									
			SourceView sourceView = new SourceView(this);
			v = sourceView;			
		}
		return v;
	}	

	
	/**
	 * Connect sourceView to a tab.
	 * 
	 * @param tag The tag of a tab or the filename.
	 * @return SourceView that will connect to tab. 
	 * */
	@Override
	public View createTabContent(String tag)
	{
		Log.i("xxxxxxxxxxx","To connect sourceView to [" + tag + "] tab");
		
		return mSourceView.get(tag);
	}
}